<?php

namespace User;

use C\L\WebUserController;
use C\M\Payment;

class InvestController extends WebUserController
{

    protected function init()
    {
        $this->service = $this->s_invest;

        $this->hideKeys = [
            'is_delete'
        ];

        $this->timeToDateKeys = [
            'uptime', 'addtime', 'ok_time'
        ];

        $this->pubSearchKeys = [
            'type', 'stype'
        ];
    }

    protected function beforeSearch()
    {
        $this->params['page_num'] = 500;
        $this->params['data']['uid'] = $this->uid;
        return true;
    }

    public function indexAction()
    {
        $this->checkAuth();
        $this->checkCanInvest();

        $level = $this->s_level->getLevel($this->uid);

        $payment = Payment::findById($level['payment_id']);
        if (!$payment) {
            return $this->error('无支付渠道');
        }

        $paymentConfig = $this->s_config->get('pay');
        if (!$paymentConfig) {
            return $this->error('无支付配置');
        }

        $config = [];
        if ($payment->is_enabled_alipay) {
            array_push($config, [
                'key'        => 'alipay',
                'name'       => '支付宝',
                'is_default' => 'N',
            ]);
        }

        if ($payment->is_enabled_wechat) {
            array_push($config, [
                'key'        => 'wx',
                'name'       => '微信支付',
                'is_default' => 'Y',
            ]);
        }

        if ($payment->is_enabled_bank) {
            array_push($config, [
                'key'        => 'bank',
                'name'       => '银行汇款',
                'is_default' => 'N',
            ]);
        }

        $this->success([
            'invest_min_money' => $paymentConfig['invest_min_money'],
            'config'           => $config,
            'amount_options'   => $payment->amount_options,
        ]);
    }

    /**
     * 返回充值二维码及描述
     */
    public function renderAction()
    {
        $this->checkAuth();
        $this->checkCanInvest();

        $money   = $this->getValue('money', true, 'float');
        $channel = $this->getValue('channel', true);

        $paymentConfig = $this->s_config->get('pay');

        if ($paymentConfig['invest_min_money'] > 0 && $money < $paymentConfig['invest_min_money']) {
            return $this->error('最小充值金额为' . $paymentConfig['invest_min_money']);
        }

        $level = $this->s_level->getLevel($this->uid);

        $payment = Payment::findById($level['payment_id']);
        if (!$payment) {
            return $this->error('无支付渠道');
        }

        $result = [
            'channel' => $channel,
        ];

        switch ($channel) {
            case 'alipay':
                if (!$payment->is_enabled_alipay) {
                    return $this->error('该渠道暂不可用');
                }
                $result = array_merge($result, [
                    'image'   => $payment->alipay_qrcode,
                    'content' => $payment->alipay_note,
                ]);
                break;
            case 'wx':
                if (!$payment->is_enabled_wechat) {
                    return $this->error('该渠道暂不可用');
                }
                $result = array_merge($result, [
                    'image'   => $payment->wechat_qrcode,
                    'content' => $payment->wechat_note,
                ]);
                break;
            case 'bank':
                if (!$payment->is_enabled_bank) {
                    return $this->error('该渠道暂不可用');
                }
                $result = array_merge($result, [
                    'bank_name'        => $payment->bank_name,
                    'bank_user'        => $payment->bank_card_name,
                    'bank_card'        => $payment->bank_card_no,
                    'bank_back_remark' => $payment->bank_note,
                ]);
                break;
            default:
                break;
        }

        return $this->success($result);
    }

    /**
     * 充值
     **/
    public function applyAction()
    {
        $this->checkAuth();
        $this->checkCanInvest();

        $money   = $this->getValue('money', true, 'float');
        $channel = $this->getValue('channel', true);
        $remark  = $this->getValue('remark', true);

        $pay = $this->s_config->get('pay');
        if ($pay['invest_min_money'] > 0 && $money < $pay['invest_min_money']) {
            $this->error('最小充值金额为' . $pay['invest_min_money']);
        }

        if (!in_array($channel, ['wx', 'alipay', 'bank'])) {
            $this->error('充值渠道不存在');
        }

        $data = [
            'uid'         => $this->uid,
            'channel'     => $channel,
            'reg_channel' => $this->s_user->getValue('channel', ['uid' => $this->uid]),
            'money'       => $money,
            'name'        => $this->ssid->get('name'),
            'remark'      => $remark,
        ];

        return $this->s_invest->save($data)
          ? $this->success()
          : $this->error('充值失败');
    }
}
